package com.wukang.photovoltaicinspectionpictures.ui.photograph

import android.view.LayoutInflater
import android.view.ViewGroup
import com.wukang.photovoltaicinspectionpictures.R
import com.wukang.photovoltaicinspectionpictures.base.BaseFragment
import com.wukang.photovoltaicinspectionpictures.databinding.FragmentPhotographResultBinding
import com.wukang.photovoltaicinspectionpictures.ui.dialog.CustomDialog
import com.wukang.photovoltaicinspectionpictures.utils.onDebounceClick


/**
 * 照片结果页
 * @author wukang
 */
class PhotographResultFragment : BaseFragment<FragmentPhotographResultBinding>() {

    companion object {
        val instance by lazy(LazyThreadSafetyMode.NONE) {
            PhotographResultFragment()
        }
    }

    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentPhotographResultBinding {
        return FragmentPhotographResultBinding.inflate(inflater, container, false)
    }

    override fun initFragment() {
        getParentActivity<TakePhotographActivity>().mModel.currentPhotographData.apply {
            mBinding.tvRiskLevel.text = riskLevel
            mBinding.tvPhotovoltaicLocation.text = photovoltaicLocation
        }
    }

    override fun FragmentPhotographResultBinding.initView() {
        btRetry.onDebounceClick {
            getParentActivity<TakePhotographActivity>().mModel.clearAndRetry()
        }
        btMark.onDebounceClick {
            showMarkDialog { getParentActivity<TakePhotographActivity>().mModel.markData(it) }
        }
        btComplete.onDebounceClick {
            getParentActivity<TakePhotographActivity>().mModel.taskComplete("", "", "")
        }
        btMarkAndNext.onDebounceClick { showMarkDialog { getParentActivity<TakePhotographActivity>().mModel.markDataAndNext(it) } }
    }

    private fun showMarkDialog(fn: (String) -> Unit) {
        CustomDialog(
            requireContext(),
            titleRes = R.string.photograph_dialog_title,
            editNameRes = R.string.photograph_dialog_edit_name,
            editHintRes = R.string.photograph_dialog_edit_hint,
            negativeRes = R.string.btn_cancel,
            positiveRes = R.string.btn_confirm,
            positiveListener = fn
        ).also { dialogDispose.add(it) }.show()
    }
}